//
//  MartinsBaseTableViewCell.m
//  NLProjectFrameworkDemo
//
//  Created by iosdeveloper on 2020/4/26.
//  Copyright © 2020 yj_zhang. All rights reserved.
//

#import "MartinsBaseTableViewCell.h"




@interface MartinsBaseTableViewCell()
@property(nonatomic,copy)NSString*msgId;
@property(nonatomic,strong)MantisCellBaseModel*currentModel;
@end


@implementation MartinsBaseTableViewCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor =[UIColor clearColor];
        self.selectionStyle=0;
        [self setUpView];
    }
    return self;
}

-(void)setUpView{
    
    UILabel*timeLabel = [UILabel new];
    self.timeLabel=timeLabel;
    timeLabel.font =KIMFontRegularSize(14);
    timeLabel.textColor=[UIColor jk_colorWithHexString:@"#C9D1D4"];
    timeLabel.textAlignment=NSTextAlignmentCenter;
    [self.contentView addSubview:timeLabel];
    [timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self);
//        make.height.mas_equalTo(30);
    }];
    
    
    UIImageView*imgView = [UIImageView new];
    self.avaterView=imgView;
    imgView.image=[UIImage imageNamed:@"chat_visitor_avater"];
    imgView.backgroundColor=[UIColor grayColor];
    [self.contentView addSubview:imgView];
   
    
    UIView*bgView =[UIView new];
    self.bgView=bgView;
    bgView.backgroundColor=[UIColor whiteColor];
    [self.contentView addSubview:bgView];
   
    
//   self.bgView.layer.backgroundColor = [UIColor colorWithRed:6/255.0 green:203/255.0 blue:148/255.0 alpha:1.0].CGColor;
//      self.bgView.layer.cornerRadius = 8;
      self.bgView.layer.shadowColor = [UIColor colorWithRed:0/255.0 green:29/255.0 blue:21/255.0 alpha:0.07].CGColor;
      self.bgView.layer.shadowOffset = CGSizeMake(0,3);
      self.bgView.layer.shadowOpacity = 1;
      self.bgView.layer.shadowRadius = 6;
  
   
    
    UILabel*contentLabel = [UILabel new];
    self.contentLabel=contentLabel;
    contentLabel.lineBreakMode = NSLineBreakByCharWrapping;
    contentLabel.font = KIMFontRegularSize(16);
    contentLabel.textColor=[UIColor jk_colorWithHexString:@"#3E464C"];
    contentLabel.numberOfLines=0;
    contentLabel.textAlignment=NSTextAlignmentLeft;
    [self.bgView addSubview:contentLabel];
 
    
    //转圈
    UIActivityIndicatorView* indicator= [UIActivityIndicatorView new];
    indicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
    [self.contentView addSubview:indicator];
    self.indicator=indicator;
    indicator.hidden=YES;
    
     //错误标识
    UIButton*errorBtn = [UIButton new];
    self.errorBtn=errorBtn;
    [errorBtn setImage:[UIImage imageNamed:@"news_msg_error"] forState:UIControlStateNormal];
    [self.contentView addSubview:errorBtn];
    errorBtn.hidden=YES;
    
    [errorBtn addTarget:self action:@selector(retrySendMessage:) forControlEvents:UIControlEventTouchUpInside];
    
    
   
}
-(void)setIsShowActivity:(BOOL)isShowActivity{
    if (isShowActivity) {
        self.indicator.hidden=NO;
         [self.indicator startAnimating];
    }else{
        [self.indicator stopAnimating];
    }
 
}


//-(void)getKnoticeWithErrorBtn:(NSNotification*)notice{
//
//    NSString*msgID=notice.userInfo[msgIDStr];
//    self.msgId=msgID;
//    if ([self.cellBaseData.innerMessage.msgId isEqualToString:msgID]) {
//        self.errorBtn.hidden=NO;
//    }else{
//        self.errorBtn.hidden=YES;
//    }
//
//}

-(void)retrySendMessage:(UIButton*)sender{
    WS(weakSelf);
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"确定重发此消息吗？" message:nil preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"重发" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if (weakSelf.currentModel==nil||!weakSelf.currentModel) {
            return ;
        }
        [[NSNotificationCenter defaultCenter]postNotificationName:KnoticeRetryNewsSend object:nil userInfo:@{@"cellData":weakSelf.currentModel}];
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

    }]];
    [self.jk_viewController.navigationController presentViewController:alert animated:YES completion:nil];
}
-(void)setThreeCorner:(CGSize)bgSize bgView:(UIView*)cornerView{
    cornerView.frame=CGRectMake(0, 0, bgSize.width, bgSize.height);
}


-(void)relayoutLabelSize:(CGSize)labelSize{
   
//   if (labelSize.width<adaptWidth750(200)) {
        [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.bgView);
            make.centerY.equalTo(self.bgView);
             make.size.mas_equalTo(labelSize);
        }];
    
         //[self.contentLabel sizeToFit];

//    }else{
//        [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.centerX.equalTo(self.bgView);
//            make.centerY.equalTo(self.bgView);
//            make.size.mas_equalTo(labelSize);
//        }];
//    }
}


-(void)reloadCellModel:(MantisCellBaseModel*)data{
    self.currentModel=data;
//    NSString*timeStr=data.historyModel.timeStr;
//    NSTimeInterval timer;
//    if (timeStr.length>0)
//    {
//       timer =[GetChatTime getTimeStampFrom:timeStr];
//    }else
//    {
//        timer=data.sendTime;
//    }
    
//   self.timeLabel.text=[GetChatTime getMessageDateStringFromTimeInterval:timer andNeedTime:YES];
//    }else{
        if (data.isTimeVisible) {
            self.timeLabel.text=[GetChatTime getMessageDateStringFromTimeInterval:data.sendTime andNeedTime:YES];
        }else{
          self.timeLabel.text=@"";
        }
//    }
    
   
    NSString*imgUrl =data.imgUrl;

    if (imgUrl.length>0) {
//        if ([imgUrl containsString:@"gif"]) {
////            NSData*imgData=[[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:data.imgUrl]];
////            YYImage*gifImg =[UIImage sd_imageWithData:imgData];
////            self.avaterView.image=gifImg;
//        }else{
            [self.avaterView setImageWithURL:[NSURL URLWithString:imgUrl] placeholder:KdefaultAVater];
//        }

    }else{
        self.avaterView.image = KdefaultAVater;
    }
    
    self.errorBtn.hidden=!data.isShowError;
    
    
  
    
}
-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
